browser = (navigator.appVersion.indexOf("MSIE") >=0) && 
		  (navigator.appVersion.toLowerCase().indexOf('win')>=0)     ?  "MSIE_win"  : 
		  (navigator.appVersion.toLowerCase().indexOf('safari') >=0) ?  "safari"    : 
		  (navigator.appVersion.toLowerCase().indexOf('firefox') >=0) ? "firefox"   : "other";

function getFlash(movieName)
{
	if(navigator.appName.indexOf('Microsoft') != -1)
	{
		return window[movieName];
	}
	else
	{
		return document[movieName];
	}
}

Object.extend(Prototype, {
	OS: {
		Win: !navigator.platform.indexOf('Win'),
		Mac: !navigator.platform.indexOf('Mac'),
		MacIntel: navigator.platform == 'MacIntel',
		MacPPC: navigator.platform == 'MacPPC'
	}
});

// Create unity class
var Unity = Class.create();

Object.extend(Unity.prototype, {
	initialize: function()
	{
		this.installed = this._detectUnity();
		this.divUnity = this._getDivUnity();
		this.iFrameUnity = this._getiFrameUnity();
		this.unity = null;
		this.downloadUrl = this._GetInstallerPath();
		this.count = 0;
		this.lastProj = -1;
	},
	
	_getUnity: function()
	{
		if(!this.iFrameUnity)
		{
			this.iFrameUnity = this._getiFrameUnity();
		}
		var reference = this.iFrameUnity.contentWindow.document;
		var objUnity;
		try
		{
			if(navigator.appVersion.indexOf('MSIE') != -1 && navigator.appVersion.toLowerCase().indexOf('win') != -1)
			{
				objUnity = reference.getElementById('UnityObject');
			}
			else if(navigator.appVersion.toLowerCase().indexOf('safari') != -1)
			{
				objUnity = reference.getElementById('UnityObject');
			}
			else
			{
				objUnity = reference.getElementById('UnityEmbed');
			}
			return objUnity;
		}
		catch (e)
		{
			return false;
		}
	},
	
	_getiFrameUnity: function()
	{
		return document.getElementById('iframeUnity');
	},
	
	_GetInstallerPath: function()
	{
		var address = '';
		if(Prototype.OS.MacIntel)
		{
			address = '../webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg';
		}
		else if(Prototype.OS.MacPPC)
		{
			address = '../webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg';
		}
		else if(Prototype.OS.Win)
		{
			address = '../webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe';
		}
		else
		{
			address = '../www.unity3d.com/index.html';
		}
		return address;
	},
	
	GetInstallerPath: function()
	{
		return this.downloadUrl;
	},

	_detectUnity: function() 
	{
		var tInstalled = false;
		if(Prototype.Browser.IE && Prototype.OS.Win)	
		{
			tInstalled = DetectUnityWebPlayerActiveX();
		}
		else
		{
			if(navigator.mimeTypes && navigator.mimeTypes['application/vnd.unity'])
			{
				if(navigator.mimeTypes['application/vnd.unity'].enabledPlugin && navigator.plugins && navigator.plugins['Unity Player'])
				{
					tInstalled = true; 
				}
			}
		}
		
		if(tInstalled == 1)
		{
			tInstalled = true;
		}
		else if(tInstalled == 0)
		{
			tInstalled = false;
		}
		
		return tInstalled;
	},
	
	DetectUnityWebPlayer: function()
	{
		navigator.plugins.refresh();
		this.installed = this._detectUnity();
		return this.installed;
	},
	
	hideUnity: function()
	{
		this.divUnity = this._getDivUnity();
		this.iFrameUnity = this._getiFrameUnity();
		
		var ua = navigator.userAgent.toLowerCase();
		
		if(ua.indexOf('firefox') != -1)//Prototype.Browser.Gecko)
		{
			if(navigator.platform.toLowerCase().indexOf('mac') != -1)
			{
				this.divUnity.style.visibility = 'visible';
				this.divUnity.style.width = '1px';
				this.divUnity.style.height = '1px';
				this.iFrameUnity.visibility = 'visible';
				this.iFrameUnity.width = '1px';
				this.iFrameUnity.height = '1px';
			}
			else
			{
				this.divUnity.style.visibility = 'hidden';
				if(browser == "MSIE_win")
					document.getElementById("divUnity").style.display = "none";
			}
		}
		else
		{
			this.divUnity.style.visibility = 'visible';
			this.divUnity.style.width = '1px';
			this.divUnity.style.height = '1px';
			this.iFrameUnity.visibility = 'visible';
			this.iFrameUnity.width = '1px';
			this.iFrameUnity.height = '1px';
		}
	},
	
	_getDivUnity: function()
	{
		return document.getElementById('divUnity');
	},	
	
	showUnity: function()
	{
		if(this.divUnity == null)
		{
			this.divUnity = this._getDivUnity();
		}
		if(this.iFrameUnity == null)
		{
			this.iFrameUnity = this._getiFrameUnity();
		}
		
		var ua = navigator.userAgent.toLowerCase();
		
		if(ua.indexOf('firefox') != -1)//Prototype.Browser.Gecko)
		{
			if(navigator.platform.toLowerCase().indexOf('mac') != -1)
			{
				this.divUnity.style.visibility = 'visible';
				this.divUnity.style.width = '800px';
				this.divUnity.style.height = '600px';
				this.iFrameUnity.visibility = 'visible';
				this.iFrameUnity.width = '800px';
				this.iFrameUnity.height = '600px';
			}
			else
			{
				this.divUnity.style.visibility = 'visible';
				if(browser == "MSIE_win")
				    document.getElementById("divUnity").style.display = "block";
					
				if(!this.unity)
				{
					this.unity = this._getUnity();
				}		
					
			}
		}
		else
		{
			this.divUnity.style.visibility = 'visible';
			this.divUnity.style.width = '800px';
			this.divUnity.style.height = '600px';
			this.iFrameUnity.visibility = 'visible';
			this.iFrameUnity.width = '800px';
			this.iFrameUnity.height = '600px';
		}

	},
	
	flashToUnity: function(obj,func,value)
	{
		if(!this.unity)
		{
			this.unity = this._getUnity();
		}
		this.unity.SendMessage(obj, func, value);
	},
	unityToFlash2: function(arg1,arg2)
	{
		var flash = getFlash('flashMovie');
		flash.messageFromUnity2(arg1,arg2);
	},
	unityToFlash3: function(arg1,arg2,arg3)
	{
		var flash = getFlash('flashMovie');
		flash.messageFromUnity3(arg1,arg2,arg3);
	},
	unityToFlash4: function(arg1,arg2,arg3,arg4)
	{
		var flash = getFlash('flashMovie');
		flash.messageFromUnity4(arg1,arg2,arg3,arg4);
	},
	unityToFlash5: function(arg1,arg2,arg3,arg4,arg5)
	{
		var flash = getFlash('flashMovie');
		flash.messageFromUnity5(arg1,arg2,arg3,arg4,arg5);
	}
});

var gameUnity = new Unity();
